package com.sen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.entity.ActivityEntity;
import com.sen.entity.UserEntity;
import com.sen.service.ActivityService;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Map;

/**
 * 活动表

 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * 查询所有活动表且分页带条件
     * @param activity
     * @return
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String,String> activity) {

        Page<ActivityEntity> page = new Page<>(Integer.parseInt(activity.get("current")),Integer.parseInt(activity.get("size")));
        //分页查询同时添加排序规则通过活动创建的时间降序查询创建的第一时间看见自己最新创建的项目
        IPage<ActivityEntity> activityList = activityService.page(page, new QueryWrapper<ActivityEntity>().orderByDesc("createdate"));
        return R.ok().put("activityList", activityList);
    }

    /**
     * 添加活动
     * @param activityEntity
     * @return
     */
    @PostMapping("/save")
    public R save(ActivityEntity activityEntity){
        activityEntity.setState(2);
        try {
            activityService.save(activityEntity);
            return R.ok();
        }catch (Exception e){
            return  R.error("系统异常");
        }
    }
    /**
     * 修改活动
     * @param activityEntity
     * @return
     */
    @PostMapping("/upd")
    public void upd(ActivityEntity activityEntity){
        activityService.update(new UpdateWrapper<ActivityEntity>().eq("aid",activityEntity.getAid()).set("enddate",activityEntity.getEnddate()));
    }
    /**
     * 结束活动
     * @param aid 活动id
     * @return
     */
    @PostMapping("/close")
    public void close(Integer aid){
        activityService.update(new UpdateWrapper<ActivityEntity>().eq("aid",aid).set("state",1));
    }
    /**
     * 通过id查询活动
     * @param aid 活动id
     * @return
     */
    @PostMapping("/activityById/{aid}")
    public R activityById(@PathVariable("aid") Integer aid){
        ActivityEntity activity = activityService.getById(aid);
        return R.ok().put("activity", activity);
    }


}
